/**
 * created by 小卷毛, 2019-09-23 023
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.af.community.smart.viewmodel

import android.annotation.SuppressLint
import com.af.community.smart.model.CommonData
import com.af.community.smart.share.BaseUrl
import com.af.community.smart.sort.CharacterParser
import com.af.community.smart.sort.PinyinComparator
import com.af.community.smart.utils.applyFlowableSchedulers
import com.lzg.extend.BaseResponse
import com.lzg.extend.jackson.JacksonConvert
import com.lzg.okrx.adapter.FlowableBody
import com.lzy.okgo.OkGo
import io.reactivex.Flowable
import java.util.*
import kotlin.collections.ArrayList

/**
 * 项目名称：Smart_Business
 * 创建人：小卷毛
 * 创建时间：2019-09-23 11:17
 */
class ContactViewModel : BaseViewModel() {

    fun getContactList(
        userId: String
    ): Flowable<ArrayList<CommonData>> =
        OkGo.post<BaseResponse<ArrayList<CommonData>>>(BaseUrl.teleDictList)
            .params(getParams("userId" to userId))
            .converter(object : JacksonConvert<BaseResponse<ArrayList<CommonData>>>() {})
            .adapt(FlowableBody())
            .compose(applyFlowableSchedulers())
            .map { it.data }
            .doOnSubscribe { showLoading() }
            .doFinally { dismissLoading() }

    @SuppressLint("DefaultLocale")
    fun seperateLists(items: ArrayList<CommonData>) {
        if (items.isNotEmpty()) {
            items.forEach {
                val letter = CharacterParser.getInstance().getSelling(it.userName)
                val sortStr = letter.substring(0 until 1).toUpperCase()
                it.letter = if (sortStr.matches("[A-Z]".toRegex())) sortStr else "#"
            }
            Collections.sort(items, PinyinComparator())
        }
    }

}